#!/usr/bin/python
# coding:utf-8
"""
封装文件操作
删除文件和获取excel行数
"""
__author__ = 'Martin Wang <357951@qq.com>'

import logging
import os
import xlrd
from robot.api import logger


def delete_file(filepath):
    """删除一个文件
    - :param filepath: 【必填】文件路径
    - :return: 无返回体
    """
    logger.info('Start to delete file: ')
    try:
        if os.path.exists(filepath):
            logger.info('Filepath is: {}'.format(filepath))
            os.remove(filepath)
            logger.info('Delete successfully')
        else:
            logger.info('The file does not exist')
    except Exception as e:
        raise Exception("Delete file failed: {}".format(e))


def get_excel_rows(filename):
    # filename:【必填】文件名，类型：string
    logger.info("filename：{}".format(filename))
    # 打开文件
    data = xlrd.open_workbook(filename)
    # 获取第一个sheet
    sheet = data.sheets()[0]
    # 获取行数
    rows = sheet.nrows
    logger.info("file rows:{}".format(rows))
    return rows